<?php 
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
ob_start("ob_gzhandler");
require('../includes/init.php');
require('./classes/control/control_ad_position.php');
//广告位模块
class AdPosition
{
	var $ad_position_c;
	/**
	* @ 构造函数,实例化adControl
	* @ __construct
	*/
	public function __construct()
	{
		$this->ad_position_c = new AdPositionControl();
	}
	/**
	* @函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	function index()
	{
		
		$url_info = $ad_info = array();
		$w_type = empty($_REQUEST['w_type'])?0:(int)$_REQUEST['w_type'];//网站类型
		$page = !empty($_REQUEST['page'])?(int)$_REQUEST['page']:1;//分页处理
		$url_info = $this->ad_position_c->get_url_info($page,'list_index',20,$w_type);//得到分页信息
		$position_info = $this->ad_position_c->get_ad_position_info('list_index',0,$w_type,$url_info['page_info']['start'],20);//得到相应分页数据
		$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
		$GLOBALS['smarty']->assign('w_type',   $w_type);
		$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
		$GLOBALS['smarty']->assign('position_info',$position_info );
		$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
		$GLOBALS['smarty']->display('ad_position_index.htm'); 
	
	}
	/**
	* @函数add_edit//增加，修改
	* @param  NULL
	* @return  string
	*/
	public function add_edit()
	{
		$w_type = empty($_REQUEST['w_type'])?0:(int)$_REQUEST['w_type'];//网站类型
		$array = array
		(
		'position_name' =>  trim($_POST['position_name']),
		'position_size' => trim($_POST['position_size']),
		'position_type' => trim($_POST['position_type']),
		'position_description' => trim($_POST['position_description']),
		'position_id' => empty($_POST['position_id'])?0:trim($_POST['position_id']),
		'position_location' =>(int)trim($_POST['position_location'])
		);
	
		if($_REQUEST['act'] == 'add')
		{
			if($_POST['sub_from'])
			{
			$rows = $this->ad_position_c->operate_ad_position('add',$array,$w_type);
			if($rows['affect_nums'] > 0)
			{
			echo  '<script type="text/javascript"> alert("增加成功！");
			window.location.href="/wondershare/ad_position.php?action=add_edit&act=edit&w_type='.$w_type.'&position_id='.(int)$rows['last_id'].'"; </script>';
			exit;
			}
			}
			$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
			$GLOBALS['smarty']->assign('w_type',   $w_type);
			$GLOBALS['smarty']->assign('title',     '添加');
			$GLOBALS['smarty']->assign('form_act',     'add');
			$GLOBALS['smarty']->display('ad_position_info.htm');
		}
		elseif($_REQUEST['act'] == 'edit')
		{
			$ad_info = array();
			if($_POST['sub_from'])
			{
				//删除队列
				queueDelete(0, $_POST['position_id']);
				
				$rows = $this->ad_position_c->operate_ad_position('edit',$array,$w_type);
				if($rows['affect_nums'] > 0)
				{
					echo  '<script type="text/javascript"> alert("编辑成功！");
					window.location.href="/wondershare/ad_position.php?action=add_edit&act=edit&w_type='.$w_type.'&position_id='.(int)$_POST['position_id'].'"; </script>';
					exit;
				}
			
			}
			if(!empty($w_type) && !empty($_GET['position_id']))
			{
				//加入队列
				queueInfo($_GET['position_id']);
			}
			$position_info = $this->ad_position_c->get_ad_position_info('position_edit',(int)$_GET['position_id'],$w_type);
			$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
			$GLOBALS['smarty']->assign('w_type',   $w_type);
			$GLOBALS['smarty']->assign('title',     '修改');
			$GLOBALS['smarty']->assign('form_act',     'edit');
			$GLOBALS['smarty']->assign('position_info',$position_info[0] );
			$GLOBALS['smarty']->display('ad_position_info.htm'); 
		
		}elseif($_REQUEST['act'] == 'is_name_exist')//ajax判断是否有重复名称出现
		{
			$where_str = !empty($_GET['position_id'])?' AND  position_id != '.$_GET['position_id']:'';
			$sql = 'SELECT count(*) AS nums  FROM ad_position WHERE position_name = "'.trim($_GET['position_name']).'"'.$where_str.' AND is_delete = 0 AND ad_web = '.$w_type;
			$nums = $GLOBALS['db']->getOne($sql);
			echo  $nums;
		}

	}

	/**
	* @函数del
	* @param  NULL
	* @return  string
	*/
	public function del()
	{
		if(!empty($_GET['checkList']))
		{
			$checkList = substr($_GET['checkList'], 0, (trim(strlen($_GET['checkList'])) - 1) );
			$this->ad_position_c->delete_ad_position($checkList);
		}
	}

	/**
	* @函数update_value
	* @param  NULL
	* @return  string
	*/
	public function update_value()//ajax改变值
	{
	
		if(!empty($_GET['position_id']) && $_GET['type'] == 'position_name' )
		{
		
			$this->ad_position_c->change_info((int)$_GET['position_id'],trim($_GET['value']),'position_name','update_name');
		}
	}

}
$action    = $_REQUEST['action']; 
$ad_position   = new AdPosition;
if(method_exists($ad_position, $action)) 
{
$ad_position->$action();
}else{
$ad_position->index();
}

?>
